// index.js
// 获取应用实例
const app = getApp();

Page({
  data: {
    images: [],
    upload_URL: 'http://127.0.0.1:9090/wechat/upload/image'
  },
  chooseImage(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        const images = this.data.images.concat(res.tempFilePaths);
        // 限制最多只能留下3张照片
        const images1 = images.length <= 3 ? images : images.slice(0, 1);
        // 开始上传到服务器
        wx.uploadFile({
          url: that.data.upload_URL,
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: function(res){
            var data = res.data;
            that.setData({
              images: images1
            });
          }, 
          fail: function(res) {
            console.log('upload failed.');
          }
        });
      }
    })
  },
  removeImage(e) {
    var that = this;
    var images = that.data.images;
    const idx = e.currentTarget.dataset.idx;
    images.splice(idx,1)
    that.setData({
      images: images
    });
  },
})
